package org.kele.k8s.sc2020.ex;

import cn.dev33.satoken.exception.SaTokenException;
import lombok.extern.slf4j.Slf4j;
import org.kele.k8s.sc2020.vo.KeleResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExhandler {

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public KeleResponse<Object> handleEx(Exception exception) {
        log.error("unkown ex happed! ", exception);
        return KeleResponse.errorBean("999", "system error!");
    }

    @ExceptionHandler(BizRuntimeExecption.class)
    @ResponseStatus(HttpStatus.OK)
    public KeleResponse<Object> handleBizRuntimeExecption(BizRuntimeExecption exception) {
        return KeleResponse.errorBean(exception.getCode(), exception.getMessage());
    }

    @ExceptionHandler(SaTokenException.class)
    @ResponseStatus(HttpStatus.OK)
    public KeleResponse<Object> handleBizRuntimeExecption(SaTokenException exception) {
        log.error("error", exception);
        return KeleResponse.errorBean("110", exception.getMessage());
    }
}
